﻿Imports Q7P1TTS.Lib
Imports ComponentFactory.Krypton.Toolkit

Public Class ServiceForm

    Dim srv As New Service()
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Public Sub New(ByVal ID As Integer)
        InitializeComponent()
        srv = ServiceController.load(ID)
    End Sub
    Private Sub ServiceForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If validateData() Then
            srv.Title = txtTitle.Text.Trim()
            srv.Description = txtDesc.Text.Trim()
            If srv.ID <= 0 Then
                srv.CreateDate = ConvertHelper.ToUnixTimestamp(DateTime.Now)
            End If
            If ServiceController.save(srv) > 0 Then
                If MessageBox.Show("New service has been successfully saved.") = DialogResult.OK Then
                    Dim mainForm As MainForm = Me.FindForm()
                    mainForm.GoToForm(New ServiceList(), "New Service List")
                End If
            Else
                MessageBox.Show("There is an unknown error found when saving data. Please try again later")
            End If
        End If
    End Sub

    Private Function validateData() As Boolean
        Dim retval As Boolean = True
        If String.IsNullOrEmpty(txtTitle.Text.Trim()) Then
            ErrorProvider1.SetError(txtTitle, "Title can not be left blank.")
            retval = retval And False
        Else : ErrorProvider1.SetError(txtTitle, "")
        End If

        If String.IsNullOrEmpty(txtDesc.Text.Trim()) Then
            ErrorProvider1.SetError(txtDesc, "Description can not be left blank.")
            retval = retval And False
        Else : ErrorProvider1.SetError(txtDesc, "")
        End If
        Return retval
    End Function

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        For Each c As Control In KryptonPanel1.Controls
            If TypeOf c Is KryptonTextBox Then
                Dim txt As KryptonTextBox = c
                txt.Text = ""
            End If
            If TypeOf c Is KryptonRichTextBox Then
                Dim txt As KryptonRichTextBox = c
                txt.Text = ""
            End If
            If TypeOf c Is KryptonComboBox Then
                Dim cbo As KryptonComboBox = c
                cbo.SelectedIndex = 0
            End If
        Next
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Dim mainForm As MainForm = Me.FindForm()
        mainForm.GoToForm(New ServiceList(), "New Service List")
    End Sub
End Class
